package com.jiuyou.storeController.adaMember;

import com.graphbuilder.math.func.LnFunction;
import com.huifu.adapay.model.SettleAccount;
import com.jiuyou.storeController.common.StoreBaseController;
import com.ruoyi.adapayBalance.domain.AdapayBalance;
import com.ruoyi.adapayBalance.service.IAdapayBalanceService;
import com.ruoyi.adapayMember.domain.AdapayMember;
import com.ruoyi.adapayMember.service.IAdapayMemberService;
import com.ruoyi.adapaySettleaccount.domain.AdapaySettleaccount;
import com.ruoyi.adapaySettleaccount.service.IAdapaySettleaccountService;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.StoreUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.entity.StoreUserRole;
import com.ruoyi.storeUser.service.IStoreUserService;
import com.ruoyi.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 支付
 *
 * @author jiuyou
 */
@Api(tags = {"汇付下adapayMember"})
@RestController
@RequestMapping("/store/adaMember")
public class AdaMemberClientController extends StoreBaseController
{
    @Autowired
    private IAdapayMemberService adaPayMemberService;
    @Autowired
    private IAdapaySettleaccountService adapaySettleaccountService;
    @Autowired
    private IAdapayBalanceService adapayBalanceService;
    @Autowired
    private ISysConfigService configService;
    @Autowired
    private IStoreUserService storeUserService;
    /**
     * 创建分账用户
     * @param phone
     * @param userName
     * @param idCard
     * @return
     */
    @PostMapping("/createMember")
    public AjaxResult createMember(String phone,String  userName,String idCard)  {
        Long  loginStoreUserId= getLoginStoreUserId();
        StoreUserRole storeUserRole=getStoreUserRole();
        Long storeId= storeUserRole.getStoreId();
        AdapayMember adapayMemberQuery=new AdapayMember();
        adapayMemberQuery.setStoreId(storeId);
        List<AdapayMember> list= adaPayMemberService.selectAdapayMemberList(adapayMemberQuery);
        if(list.size()>0){
            return AjaxResult.error("请勿重复实名认证");
        }
        String member_id=storeId+"_"+ new Date().getTime();
        Map<String, Object>  member=adaPayMemberService.createMember(member_id,phone,userName,idCard);
        String status=(String)member.get("status");
        if("succeeded".equals(status)){
            //创建成功
            AdapayMember  adapayMember=new AdapayMember();
            adapayMember.setIdCard(idCard);
            adapayMember.setPhone(phone);
            adapayMember.setStoreId(storeId);
            adapayMember.setStoreUserId(loginStoreUserId);
            adapayMember.setAdapayMemberId(member_id);
            adapayMember.setUserName(userName);
            adaPayMemberService.insertAdapayMember(adapayMember);
            AdapayBalance adapayBalance=new AdapayBalance();
            adapayBalance.setStoreId(storeId);
            if(null==adapayBalanceService.getAdapayBalanceByStoreId(storeId)){
                adapayBalanceService.insertAdapayBalance(adapayBalance);
            }

            return AjaxResult.success("实名认证成功");
        }
        return AjaxResult.error((String)member.get("error_msg"));
    }



    /**
     * 获取当前用户商户实名信息
     * @return
     */
    @PostMapping("/getMember")
    public AjaxResult getMember()  {
        StoreUserRole storeUserRole=getStoreUserRole();
        if(null == storeUserRole) {
            return AjaxResult.error();
        }
        Long  loginStoreUserId= getLoginStoreUserId();
        Long storeId= storeUserRole.getStoreId();
        AdapayMember adapayMemberQuery=new AdapayMember();
        adapayMemberQuery.setStoreId(storeId);
        adapayMemberQuery.setStoreUserId(loginStoreUserId);
        List<AdapayMember> list= adaPayMemberService.selectAdapayMemberList(adapayMemberQuery);
        AdapayMember result=null;
        if(list.size()>0){
            result=list.get(0);
            result.setIdCard(des(result.getIdCard(),"idCard"));
        }
        return AjaxResult.success(result);
    }

    /**
     * 删除当前商户实名信息
     * @return
     */
    @PostMapping("/delMember")
    public AjaxResult delMember()  {
        StoreUserRole storeUserRole=getStoreUserRole();
        if(null == storeUserRole) {
            return AjaxResult.error();
        }
        Long storeId= storeUserRole.getStoreId();

        AdapaySettleaccount adapaySettleaccountQuery=new AdapaySettleaccount();
        adapaySettleaccountQuery.setStoreId(storeId);
        List<AdapaySettleaccount>  listSettleaccount= adapaySettleaccountService.selectAdapaySettleaccountList(adapaySettleaccountQuery);
        if(listSettleaccount.size()>0){
            return AjaxResult.error("已绑结算卡禁止删除");
        }

        AdapayMember adapayMemberQuery=new AdapayMember();
        adapayMemberQuery.setStoreId(storeId);
        List<AdapayMember> list= adaPayMemberService.selectAdapayMemberList(adapayMemberQuery);
        AdapayMember result=null;
        if(list.size()>0){
            result=list.get(0);
            Long[] ids=new Long[]{result.getId()};
            adaPayMemberService.logicDeleteAdapayMember(ids, DicConstants.BUSINESS_Business.getCode(), storeUserRole.getStoreUserId());
        }
        return AjaxResult.success();
    }
    @GetMapping("/getMemberBalance")
    public AjaxResult getMemberBalance(String member_id)  {
        String app_id=configService.selectConfigByKey("adpay_app_id");
        Map<String, Object> queryParams = new HashMap<String, Object>();
        queryParams.put("member_id", member_id);
        queryParams.put("app_id", app_id);
        Map<String, Object> settleCount=null;
        try{
             settleCount = SettleAccount.balance(queryParams);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        return AjaxResult.success(settleCount);
    }
    /**
     * 创建结算账户
     * @param bankCard
     * @param phone
     * @return
     */
    @PostMapping("/createSettleAccount")
    public AjaxResult createSettleAccount(String bankCard,String phone)  {
        Long  loginStoreUserId= getLoginStoreUserId();
        StoreUserRole storeUserRole=getStoreUserRole();
       Long storeId= storeUserRole.getStoreId();

       bankCard = bankCard.replaceAll(" ","");
        AdapayMember adapayMemberQuery=new AdapayMember();
        adapayMemberQuery.setStoreId(storeId);
        List<AdapayMember> list= adaPayMemberService.selectAdapayMemberList(adapayMemberQuery);
        AdapayMember adapayMember=null;
        if(list.size()>0){
            adapayMember=list.get(0);
        }else{
            return error("请先实名后再绑定结算卡");
        }
        AdapaySettleaccount adapaySettleaccountQuery=new AdapaySettleaccount();
        adapaySettleaccountQuery.setStoreId(storeId);
        List<AdapaySettleaccount>  listacc= adapaySettleaccountService.selectAdapaySettleaccountList(adapaySettleaccountQuery);
        if(listacc.size()>0){
            return error("已有结算卡，请勿重复绑卡");
        }
        String userName=adapayMember.getUserName();
        String idCard=adapayMember.getIdCard();
        String member_id=adapayMember.getAdapayMemberId();
        Map<String, Object>  settleAccount=adapaySettleaccountService.createSettleAccount(member_id,bankCard,userName,idCard,phone);
        String status=(String)settleAccount.get("status");
        if("succeeded".equals(status)){
            //创建成功
            String id= (String)settleAccount.get("id");
            AdapaySettleaccount adapaySettleaccount=new AdapaySettleaccount();
            adapaySettleaccount.setBankCard(bankCard);
            adapaySettleaccount.setIdCard(idCard);
            adapaySettleaccount.setPhone(phone);
            adapaySettleaccount.setSettleAccountId(id);
            adapaySettleaccount.setStoreId(storeId);
            adapaySettleaccount.setAdapayMemberId(member_id);
            adapaySettleaccount.setStoreUserId(loginStoreUserId);
            adapaySettleaccount.setUserName(userName);
            adapaySettleaccountService.insertAdapaySettleaccount(adapaySettleaccount);
            return AjaxResult.success("结算卡绑定成功");
        }
        return AjaxResult.error((String)settleAccount.get("error_msg"));
    }

    /**
     * 删除结算账号
     * @return
     */
    @PostMapping("/delSettleAccount")
    public AjaxResult delSettleAccount()  {
        Long  loginStoreUserId= getLoginStoreUserId();
        StoreUserRole storeUserRole=getStoreUserRole();
        Long storeId= storeUserRole.getStoreId();

        AdapaySettleaccount adapaySettleaccountQuery=new AdapaySettleaccount();
        adapaySettleaccountQuery.setStoreId(storeId);
        List<AdapaySettleaccount>  listacc= adapaySettleaccountService.selectAdapaySettleaccountList(adapaySettleaccountQuery);
        AdapaySettleaccount adapaySettleaccount=null;
        if(listacc.size()>0){
            adapaySettleaccount=listacc.get(0);
        }else{
            return AjaxResult.success();
        }
        String member_id=adapaySettleaccount.getAdapayMemberId();
        String settle_account_id=adapaySettleaccount.getSettleAccountId();
        Map<String, Object>  settleAccount=adapaySettleaccountService.delSettleAccount(member_id,settle_account_id);
        String status=(String)settleAccount.get("status");
        if("succeeded".equals(status)){
            //创建成功
            adapaySettleaccountService.logicDeleteAdapaySettleaccount(new Long[]{adapaySettleaccount.getId()},null,null);
            return AjaxResult.success();
        }
        return AjaxResult.error((String)settleAccount.get("error_msg"));
    }






    /**
     * 创建分账用户(员工端)
     * @param phone
     * @param userName
     * @param idCard
     * @return
     */
    @PostMapping("/createClerkMember")
    public AjaxResult createClerkMember(String phone,String  userName,String idCard)  {
        Long  loginStoreUserId= getLoginStoreUserId();

        StoreUser storeUser= storeUserService.selectStoreUserById(loginStoreUserId);
        Long storeId = storeUser.getStoreId();
        //System.out.println(storeId);
//        StoreUserRole storeUserRole=getStoreUserRole();
//        Long storeId= storeUserRole.getStoreId();
        AdapayMember adapayMemberQuery=new AdapayMember();
        adapayMemberQuery.setStoreId(storeId);
        adapayMemberQuery.setStoreUserId(loginStoreUserId);
        List<AdapayMember> list= adaPayMemberService.selectAdapayMemberList(adapayMemberQuery);
        if(list.size()>0){
            return AjaxResult.error("请勿重复实名认证");
        }
        String member_id=storeId+"_"+ new Date().getTime();
        Map<String, Object>  member=adaPayMemberService.createMember(member_id,phone,userName,idCard);
        String status=(String)member.get("status");
        if("succeeded".equals(status)){
            //创建成功
            AdapayMember  adapayMember=new AdapayMember();
            adapayMember.setIdCard(idCard);
            adapayMember.setPhone(phone);
            adapayMember.setStoreId(storeId);
            adapayMember.setStoreUserId(loginStoreUserId);
            adapayMember.setAdapayMemberId(member_id);
            adapayMember.setUserName(userName);
            adaPayMemberService.insertAdapayMember(adapayMember);
            AdapayBalance adapayBalance=new AdapayBalance();
            adapayBalance.setStoreId(storeId);
            if(null==adapayBalanceService.getAdapayBalanceByStoreId(storeId)){
                adapayBalanceService.insertAdapayBalance(adapayBalance);
            }

            return AjaxResult.success("实名认证成功");
        }
        return AjaxResult.error((String)member.get("error_msg"));
    }

    /**
     * 获取当前用户实名信息
     * @return
     */
    @PostMapping("/getClerkMember")
    public AjaxResult getClerkMember()  {
//        StoreUserRole storeUserRole=getStoreUserRole();
//        if(null == storeUserRole) {
//            return AjaxResult.error();
//        }
        Long  loginStoreUserId= getLoginStoreUserId();
        StoreUser storeUser= storeUserService.selectStoreUserById(loginStoreUserId);
        Long storeId = storeUser.getStoreId();
        //Long storeId= storeUserRole.getStoreId();
        AdapayMember adapayMemberQuery=new AdapayMember();
        adapayMemberQuery.setStoreId(storeId);
        adapayMemberQuery.setStoreUserId(loginStoreUserId);
        List<AdapayMember> list= adaPayMemberService.selectAdapayMemberList(adapayMemberQuery);
        AdapayMember result=null;
        if(list.size()>0){
            result=list.get(0);
            result.setIdCard(des(result.getIdCard(),"idCard"));
        }else{
            return AjaxResult.error("尚未用户实名信息");
        }
        return AjaxResult.success(result);
    }

    /**
     * 删除当前商户实名信息(员工端)
     * @return
     */
    @PostMapping("/delClerkMember")
    public AjaxResult delClerkMember()  {
//        StoreUserRole storeUserRole=getStoreUserRole();
//        if(null == storeUserRole) {
//            return AjaxResult.error();
//        }
//        Long storeId= storeUserRole.getStoreId();

        Long  loginStoreUserId= getLoginStoreUserId();
        StoreUser storeUser= storeUserService.selectStoreUserById(loginStoreUserId);
        Long storeId = storeUser.getStoreId();

        AdapaySettleaccount adapaySettleaccountQuery=new AdapaySettleaccount();
        adapaySettleaccountQuery.setStoreId(storeId);
        adapaySettleaccountQuery.setStoreUserId(loginStoreUserId);
        List<AdapaySettleaccount>  listSettleaccount= adapaySettleaccountService.selectAdapaySettleaccountList(adapaySettleaccountQuery);
        if(listSettleaccount.size()>0){
            return AjaxResult.error("已绑结算卡禁止删除");
        }

        AdapayMember adapayMemberQuery=new AdapayMember();
        adapayMemberQuery.setStoreId(storeId);
        List<AdapayMember> list= adaPayMemberService.selectAdapayMemberList(adapayMemberQuery);
        AdapayMember result=null;
        if(list.size()>0){
            result=list.get(0);
            Long[] ids=new Long[]{result.getId()};
            adaPayMemberService.logicDeleteAdapayMember(ids, DicConstants.BUSINESS_Business.getCode(), loginStoreUserId);
        }
        return AjaxResult.success();
    }



    /**
     * 获取当前商户结算账户信息
     * @return
     */
    @PostMapping("/getSettleAccount")
    public AjaxResult getSettleAccount()  {

        StoreUserRole storeUserRole=getStoreUserRole();
        if(null == storeUserRole) {
            return AjaxResult.error();
        }
        AdapaySettleaccount result=null;
        Long storeId= storeUserRole.getStoreId();
        AdapaySettleaccount adapaySettleaccountQuery=new AdapaySettleaccount();
        adapaySettleaccountQuery.setStoreId(storeId);
        List<AdapaySettleaccount>  list= adapaySettleaccountService.selectAdapaySettleaccountList(adapaySettleaccountQuery);
        if(list.size()>0){
            result=list.get(0);
            result.setBankCard(this.des(result.getBankCard(),"bankCard"));
            result.setIdCard(des(result.getIdCard(),"idCard"));
        }
        return AjaxResult.success(result);
    }
    private String des(String val,String type){
        if(StringUtils.isEmpty(val)){
            return null;
        }
        return val.replaceAll("(?<=\\w{3})\\w(?=\\w{4})", "*");
    }


    /**
     * 创建结算账户(员工端)
     * @param bankCard
     * @param phone
     * @return
     */
    @PostMapping("/createClerkSettleAccount")
    public AjaxResult createClerkSettleAccount(String bankCard,String phone)  {
        Long  loginStoreUserId= getLoginStoreUserId();
//        StoreUserRole storeUserRole=getStoreUserRole();
//        Long storeId= storeUserRole.getStoreId();
        StoreUser storeUser= storeUserService.selectStoreUserById(loginStoreUserId);
        Long storeId = storeUser.getStoreId();

        bankCard = bankCard.replaceAll(" ","");
        AdapayMember adapayMemberQuery=new AdapayMember();
        adapayMemberQuery.setStoreId(storeId);
        List<AdapayMember> list= adaPayMemberService.selectAdapayMemberList(adapayMemberQuery);
        AdapayMember adapayMember=null;
        if(list.size()>0){
            adapayMember=list.get(0);
        }else{
            return error("请先实名后再绑定结算卡");
        }
        AdapaySettleaccount adapaySettleaccountQuery=new AdapaySettleaccount();
        adapaySettleaccountQuery.setStoreId(storeId);
        adapaySettleaccountQuery.setStoreUserId(loginStoreUserId);
        List<AdapaySettleaccount>  listacc= adapaySettleaccountService.selectAdapaySettleaccountList(adapaySettleaccountQuery);
        if(listacc.size()>0){
            return error("已有结算卡，请勿重复绑卡");
        }
        String userName=adapayMember.getUserName();
        String idCard=adapayMember.getIdCard();
        String member_id=adapayMember.getAdapayMemberId();
        Map<String, Object>  settleAccount=adapaySettleaccountService.createSettleAccount(member_id,bankCard,userName,idCard,phone);
        String status=(String)settleAccount.get("status");
        if("succeeded".equals(status)){
            //创建成功
            String id= (String)settleAccount.get("id");
            AdapaySettleaccount adapaySettleaccount=new AdapaySettleaccount();
            adapaySettleaccount.setBankCard(bankCard);
            adapaySettleaccount.setIdCard(idCard);
            adapaySettleaccount.setPhone(phone);
            adapaySettleaccount.setSettleAccountId(id);
            adapaySettleaccount.setStoreId(storeId);
            adapaySettleaccount.setAdapayMemberId(member_id);
            adapaySettleaccount.setStoreUserId(loginStoreUserId);
            adapaySettleaccount.setUserName(userName);
            adapaySettleaccountService.insertAdapaySettleaccount(adapaySettleaccount);
            return AjaxResult.success("结算卡绑定成功");
        }
        return AjaxResult.error((String)settleAccount.get("error_msg"));
    }


    /**
     * 获取当前商户结算账户信息(员工端)
     * @return
     */
    @PostMapping("/getClerkSettleAccount")
    public AjaxResult getClerkSettleAccount()  {
        Long  loginStoreUserId= getLoginStoreUserId();
        System.out.println(loginStoreUserId);
        StoreUser storeUser= storeUserService.selectStoreUserById(loginStoreUserId);
        Long storeId = storeUser.getStoreId();

//        StoreUserRole storeUserRole=getStoreUserRole();
//        if(null == storeUserRole) {
//            return AjaxResult.error();
//        }
        AdapaySettleaccount result=null;
//        Long storeId= storeUserRole.getStoreId();

        AdapaySettleaccount adapaySettleaccountQuery=new AdapaySettleaccount();
        adapaySettleaccountQuery.setStoreId(storeId);
        adapaySettleaccountQuery.setStoreUserId(loginStoreUserId);
        List<AdapaySettleaccount>  list= adapaySettleaccountService.selectAdapaySettleaccountList(adapaySettleaccountQuery);
        System.out.println("tangyinddq");
        System.out.println(list);
        if(list.size()>0){
            result=list.get(0);
            result.setBankCard(this.des(result.getBankCard(),"bankCard"));
            result.setIdCard(des(result.getIdCard(),"idCard"));
        }else{

            return AjaxResult.error("当前用户未添加结算信息");
        }
        return AjaxResult.success(result);
    }

    /**
     * 删除结算账号(员工端)
     * @return
     */
    @PostMapping("/delClerkSettleAccount")
    public AjaxResult delClerkSettleAccount()  {
        Long  loginStoreUserId= getLoginStoreUserId();
        StoreUser storeUser= storeUserService.selectStoreUserById(loginStoreUserId);
        Long storeId = storeUser.getStoreId();
//        StoreUserRole storeUserRole=getStoreUserRole();
//        Long storeId= storeUserRole.getStoreId();

        AdapaySettleaccount adapaySettleaccountQuery=new AdapaySettleaccount();
        adapaySettleaccountQuery.setStoreId(storeId);
        adapaySettleaccountQuery.setStoreUserId(loginStoreUserId);
        System.out.println("yin");
        System.out.println(storeId);
        System.out.println(loginStoreUserId);
        List<AdapaySettleaccount>  listacc= adapaySettleaccountService.selectAdapaySettleaccountList(adapaySettleaccountQuery);
        System.out.println(listacc);
        System.out.println(listacc.size());
        AdapaySettleaccount adapaySettleaccount=null;
        if(listacc.size()>0){
            adapaySettleaccount=listacc.get(0);
        }else{
            return AjaxResult.success();
        }
        String member_id=adapaySettleaccount.getAdapayMemberId();
        String settle_account_id=adapaySettleaccount.getSettleAccountId();

        System.out.println("tang");
        System.out.println(member_id);
        System.out.println(settle_account_id);

        Map<String, Object>  settleAccount=adapaySettleaccountService.delSettleAccount(member_id,settle_account_id);
        String status=(String)settleAccount.get("status");
        if("succeeded".equals(status)){
            //创建成功
            adapaySettleaccountService.logicDeleteAdapaySettleaccount(new Long[]{adapaySettleaccount.getId()},null,null);
            return AjaxResult.success();
        }
        return AjaxResult.error((String)settleAccount.get("error_msg"));
    }

}
